package com.weng.platform.stories.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.common.model.component.option.SelectOption;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.stories.model.dto.CourseDto;
import com.weng.platform.stories.model.req.CourseListReq;
import com.weng.platform.stories.model.vo.IterationsVo;
import com.weng.platform.stories.service.IIterationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 课程信息 前端控制器
 *
 * @author wenghui
 * @date 2020-05-17 13:16:31
 *
 */  
@Slf4j
@RestController
@RequestMapping("/iterations")
@Api(value = "课程信息api", tags = {"课程信息操作接口"})
public class IterationsApi extends BaseController {
    @Autowired
    private IIterationsService courseService;



    /**
     * 获取下拉框
     */
    @ApiOperation(value = "获取下拉框", notes = "")
    @GetMapping(value = "/select-option")
    public ResponseData selectOption( ){
        CourseListReq paramData = new CourseListReq();
        paramData.setIsPublish("1");
        IPage<CourseDto> iPage = courseService.list(1, 1000, paramData);
        List<CourseDto> list = iPage.getRecords();
        List<SelectOption> options = new ArrayList<>();
        if(list != null){
            for(CourseDto courseDto : list){
                SelectOption option = new SelectOption(courseDto.getId(), courseDto.getTitle());
                options.add(option);
            }
        }
        return ResponseData.success(options);

    }






    /**
     * 功能描述:单个获取课程信息
     */
    @ApiOperation(value = "获取课程信息", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        IterationsVo course = courseService.getCourseRel(id);
        return success(course);
    }


    /**
     * 条件 分页查询课程信息
     */
    @ApiOperation(value = "分页获取管理课程信息", notes = "")
    @GetMapping(value = "/list-manager")
    public ResponseData listOfManager(Integer pageNo,Integer pageSize,CourseListReq paramData){
        IPage<CourseDto> iPage = courseService.list(pageNo, pageSize, paramData);
        List<CourseDto> list = iPage.getRecords();
        return success(iPage);
    }

    /**
     * 条件 分页查询课程信息
     */
    @ApiOperation(value = "分页获取课程信息", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize,CourseListReq paramData){
		IPage<CourseDto> iPage = courseService.list(pageNo, pageSize, paramData);
        return success(iPage);
    }



    /**
	 * 新增课程信息
	 */
	@ApiOperation(value = "新增课程信息", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody IterationsVo courseReq){

		courseService.add(courseReq);
		return success();
	}




	/**
	 * 更新课程信息
	 */
	@ApiOperation(value = "更新课程信息", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody IterationsVo courseReq){
		int flag = courseService.update(courseReq);
        return success(flag);
	}


	/**
	 * 删除课程信息
	 */
	@ApiOperation(value = "ids 删除课程信息", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		boolean flag = courseService.removeByIds(Arrays.asList(ids.split(",")));
		if(flag){
			return success(deleteReq.getIds());
		}else{
			return failed("删除失败");
		}
	}

    /**
     * 删除课程信息
     */
    @ApiOperation(value = "ids 删除课程信息", notes = "可以删除多个，中间以,号分隔")
    @PostMapping(value = "/del")
    public ResponseData del(String id){
        courseService.removeById(id);
        return ResponseData.success();
    }
}
